package com.fsc.fragment;

import java.util.List;

import com.fsc.fragment.outcome.OutCome;
import com.fsc.fragment.outcome.OutComeDao;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class OutcomeActivity extends Fragment implements OnClickListener{
	private TextView btn_outCome;
	private ListView lv_outcome;
	private OutComeAdapter adapter;
	private List<OutCome> data;
	private OutComeDao Dao;
	private int position;
	private View v;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.activity_outcome,container, false);
		
		//给变量初始化
		lv_outcome = (ListView) v.findViewById(R.id.lv_outcome);
		btn_outCome = (TextView)v.findViewById(R.id.btn_outCome);
		
		adapter = new OutComeAdapter();
		Dao = new OutComeDao(getActivity());
		
		//查询
		data = Dao.query();
		adapter.notifyDataSetChanged();
		lv_outcome.setAdapter(adapter);
		
		//给btn_outCome设置监听
		btn_outCome.setOnClickListener(this);
		//给lv_income设置长按监听
		lv_outcome.setOnCreateContextMenuListener(this);
		return v;
	}
	
	//继续
	@Override
	public void onResume() {
		super.onResume();
		data.clear();
		data = Dao.query();
		adapter.notifyDataSetChanged();
		lv_outcome.setAdapter(adapter);
	}
	//创建适配器
	class OutComeAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHodler hodler=null;
			if(convertView == null){//判断缓存对象是否为null,不为空时已经缓存了对象
				hodler=new ViewHodler();
				convertView=View.inflate(getActivity(),R.layout.item_outcome, null);
				hodler.tv_money=(TextView)convertView.findViewById(R.id.tv_money);
				hodler.tv_address=(TextView)convertView.findViewById(R.id.tv_address);
				hodler.tv_time=(TextView)convertView.findViewById(R.id.tv_time);
				convertView.setTag(hodler);
				
			}else{//等于null说明第一次显示，则TextView都是新创建的
				hodler=(ViewHodler)convertView.getTag();
			}
			OutCome outCome = data.get(position);

			hodler.tv_money.setText(""+outCome.getMoney());
			hodler.tv_address.setText(outCome.getAddress());
			hodler.tv_time.setText(outCome.getTime());
			return convertView;
		}	
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("请选择操作！");
		menu.setHeaderIcon(R.drawable.ic_launcher);
		menu.add(0, 1, 0, "编辑");
		menu.add(0, 2, 0, "删除");
		
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
		position = info.position;
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		final OutCome outCome = data.get(position);
		switch (item.getItemId()) {
		case 1://1编辑
			int id = outCome.getId();
			Intent intent = new Intent(getActivity(),AddOutComeActivity.class);
			intent.putExtra("id",id);
			intent.putExtra("money",outCome.getMoney()+"");
			intent.putExtra("time",outCome.getTime());
			intent.putExtra("sp_type", outCome.getType());
			intent.putExtra("address", outCome.getAddress());
			intent.putExtra("mark", outCome.getMark());
			intent.putExtra("operate",1);//operate是区分新增还是编辑operate=1（编辑）3（新增）
			startActivityForResult(intent,1);
			break;

		default://2删除
			new AlertDialog.Builder(getActivity())
			.setTitle("是否确定删除？")
			.setPositiveButton("确定",new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					//1.删除数据表对应的数据
					Dao.delete(outCome.getId());
						//2.删除List对应的数据
					data.remove(position);
						//3.通知更新列表
					adapter.notifyDataSetChanged();
				}
			})
			//3.取消按钮
			.setNegativeButton("取消", null)
			.show();
			break;
		}
		return super.onContextItemSelected(item);
	};
	
	@Override
	public void onClick(View v) {//3.新增
		if(v == btn_outCome){
			Intent Intent = new Intent(getActivity(),AddOutComeActivity.class);
			Intent.putExtra("operate", 3);//operate是区分新增还是编辑operate=1（编辑）3（新增）
			startActivityForResult(Intent,3);//一开始是1
		}
	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if(requestCode == 3 && resultCode == 3){//一开始是1
			OutCome outCome = new OutCome();
			//1、设置数据
			outCome.setMoney(Double.parseDouble(intent.getStringExtra("money")));
			outCome.setTime(intent.getStringExtra("time"));
			outCome.setType(intent.getStringExtra("type"));
			outCome.setAddress(intent.getStringExtra("address"));
			outCome.setMark(intent.getStringExtra("mark"));
			//2、执行添加操作
			Dao.insert(outCome);
			//3、添加到list中
			data.add(0,outCome);
			//4、通知list更新
			adapter.notifyDataSetChanged();
			Toast.makeText(getActivity(), "新增收入成功！", Toast.LENGTH_LONG).show();
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}
}
